package com.qupai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qupai.constant.StatusConstant;
import com.qupai.domain.dto.AuctioneerDTO;
import com.qupai.domain.dto.AuctioneerLoginDTO;
import com.qupai.domain.entity.Auctioneer;
import com.qupai.domain.vo.AuctioneerLoginVO;
import com.qupai.domain.vo.AuctioneerVO;
import com.qupai.exception.AuctioneerException;
import com.qupai.mapper.AuctioneerMapper;
import com.qupai.properties.JwtClaimsConstant;
import com.qupai.properties.JwtProperties;
import com.qupai.service.IAuctioneerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.utils.BaseContext;
import com.qupai.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 拍卖师表，存储拍卖师信息 服务实现类
 * </p>
 *
 * @author bluefoxyu
 * @since 2024-07-30
 */
@Service
@RequiredArgsConstructor
public class AuctioneerServiceImpl extends ServiceImpl<AuctioneerMapper, Auctioneer> implements IAuctioneerService {

    private final AuctioneerMapper auctioneerMapper;
    private final JwtProperties jwtProperties;


    @Override
    public AuctioneerLoginVO login(AuctioneerLoginDTO auctioneerLoginDTO) {
        String username= auctioneerLoginDTO.getUsername();
        String password= auctioneerLoginDTO.getPassword();

        // 根据用户名查询数据库
        QueryWrapper<Auctioneer> wrapper=new QueryWrapper<>();
        wrapper.lambda()
                .eq(Auctioneer::getUsername,username);
        Auctioneer auctioneer = auctioneerMapper.selectOne(wrapper);

        // 判断查询结果是否为null,根据名字查询如果账户里面没有那个人则退出查询
        // 如果为null，则抛出AccountNotFoundException异常，并返回错误信息
        if (auctioneer == null) {
            throw new AuctioneerException("账号不存在");
        }
        // 判断密码是否正确，如果密码不正确，则抛出异常，并返回错误信息
        //密码比对
        boolean flag= password.equals(auctioneer.getPassword());
        if (!flag) {
            throw new AuctioneerException("密码错误");
        }

        if (auctioneer.getStatus().equals(StatusConstant.DISABLE)){
            //账号被锁定
            throw new AuctioneerException("账号被锁定");
        }

        //登录成功后返回jwt令牌
        Map<String,Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.AUCTION_ID,auctioneer.getId());

        //生成token，注入相关jwtProperties配置属性
        String token= JwtUtil.createJWT(
                jwtProperties.getAuctionSecretKey(),
                jwtProperties.getAuctionTtl(),
                claims);

        // 返回token
        return AuctioneerLoginVO.builder()
                .id(auctioneer.getId())
                .username(auctioneer.getUsername())
                .token(token).build();
    }

    @Override
    public AuctioneerVO info() {
        Auctioneer auctioneer = auctioneerMapper.selectById(BaseContext.getCurrentId());

        String phone = auctioneer.getPhone();
        auctioneer.setPhone(phone.substring(0,3) + "****" + phone.substring(7));
        auctioneer.setPassword("******");

        AuctioneerVO auctioneerVO = new AuctioneerVO();
        BeanUtils.copyProperties(auctioneer,auctioneerVO);

        return auctioneerVO;
    }

    @Override
    public void update(AuctioneerDTO auctioneerDTO) {

        Auctioneer auctioneer = new Auctioneer();
        auctioneer.setId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(auctioneerDTO, auctioneer);
        auctioneerMapper.updateById(auctioneer);
    }
}
